/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bu.android.bluetoothlegatt;

import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class includes a small subset of standard GATT attributes for demonstration purposes.
 */
public class SampleGattAttributes {
    private static HashMap<String, String> attributes = new HashMap();
    private static final Pattern regex_uuid16 = Pattern.compile("0000([0-9a-fA-F]{4})-0000-1000-8000-00805f9b34fb");
    public static String HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_SERVICE_NORMAL  = "0000f000-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_NORMAL_NOTIFY = "0000f001-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_NORMAL_WRITE = "0000f002-0000-1000-8000-00805f9b34fb";

    public static final String XCOBRA_SERVICE_CALIB   = "0000f010-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_CALIB_NOTIFY   = "0000f011-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_CALIB_WRITE   = "0000f012-0000-1000-8000-00805f9b34fb";


    public static final String XCOBRA_SERVICE_CALIB_CHECK = "0000f020-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_CALIB_CHECK_NOTIFY = "0000f021-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_CALIB_CHECK_WRITE = "0000f022-0000-1000-8000-00805f9b34fb";

    public static final String XCOBRA_SERVICE_BLOB_CFG = "0000f100-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_BLOB_CFG_READ = "0000f101-0000-1000-8000-00805f9b34fb";
    public static final String XCOBRA_CHAR_BLOB_CFG_WRITE = "0000f102-0000-1000-8000-00805f9b34fb";

    public static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

    static {
        // Sample Services.
        attributes.put("00001800-0000-1000-8000-00805f9b34fb", "Generic Access");
        attributes.put("00001801-0000-1000-8000-00805f9b34fb", "Generic Attribute");
        attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information Service");
        attributes.put("0000180f-0000-1000-8000-00805f9b34fb", "Battery Service");
        attributes.put(XCOBRA_SERVICE_NORMAL, "CobraNormal");
        attributes.put(XCOBRA_SERVICE_CALIB, "CobraCalib");
        attributes.put(XCOBRA_SERVICE_CALIB_CHECK, "CobraCalibCheck");
        attributes.put(XCOBRA_SERVICE_BLOB_CFG, "CobraBlobCfg");
        // Sample Characteristics.
        attributes.put("00002a25-0000-1000-8000-00805f9b34fb", "Serial Number String");
        attributes.put("00002a27-0000-1000-8000-00805f9b34fb", "Hardware Revision String");
        attributes.put("00002a28-0000-1000-8000-00805f9b34fb", "Software Revision String");
        attributes.put("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name String");
        attributes.put("00002a19-0000-1000-8000-00805f9b34fb", "Battery Level");

        attributes.put(XCOBRA_CHAR_NORMAL_NOTIFY, "Notify");
        attributes.put(XCOBRA_CHAR_NORMAL_WRITE, "Write");
        attributes.put(XCOBRA_CHAR_CALIB_NOTIFY, "Notify");
        attributes.put(XCOBRA_CHAR_CALIB_WRITE, "Write");
        attributes.put(XCOBRA_CHAR_CALIB_CHECK_NOTIFY, "Notify");
        attributes.put(XCOBRA_CHAR_CALIB_CHECK_WRITE, "Write");
        attributes.put(XCOBRA_CHAR_BLOB_CFG_READ, "Read");
        attributes.put(XCOBRA_CHAR_BLOB_CFG_WRITE, "Write");
    }

    public static String lookup(String uuid, String defaultName) {
        String name = attributes.get(uuid);
        return name == null ? defaultName : name;
    }

    public static int GetUUID16BitFromString(String uuidString)
    {
        Matcher m = regex_uuid16.matcher(uuidString);
        if(m.find())
        {
            return Integer.parseInt(m.group(1), 16);
        }
        else
        {
            return 0;
        }
    }
}
